Skip to content

Conversation

bvanjoi
Copy link
Contributor

@bvanjoi bvanjoi commented Jun 28, 2025

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 28, 2025
@petrochenkov
Copy link
Contributor

I don't understand why this works and how it fixes the issue.
The LookAheadMacroDefinition are inserted for all macro definitions, regardless of whether it's macro or macro_rules, and nothing is done for use items.
The reversed rib walk in fn apply_pattern_bindings can also encounter arbitrary ribs before encountering a LookAheadMacroDefinition.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 30, 2025
@petrochenkov
Copy link
Contributor

Also "shallow" -> "shadow" in the PR/commit messages and file names.

@bvanjoi
Copy link
Contributor Author

bvanjoi commented Jul 1, 2025

The reversed rib walk in fn apply_pattern_bindings can also encounter arbitrary ribs before encountering a LookAheadMacroDefinition.

There may be a bug if LookAheadMacroDefinition fails to apply correctly.

I don't understand why this works and how it fixes the issue.

LookAheadMacroDefinition stores the macro expansion result and serves as a fallback in resolve_ident_in_lexical_scope, ensuring it takes the correct resolution path:

// The ident resolves to a type parameter or local variable.
return Some(LexicalScopeBinding::Res(self.validate_res_from_ribs(
i,
rib_ident,
*res,
finalize.map(|finalize| finalize.path_span),
*original_rib_ident_def,
ribs,
)));

@bvanjoi bvanjoi changed the title fresh binding should shallow the def after expand fresh binding should shadow the def in expand Jul 1, 2025
@bvanjoi
Copy link
Contributor Author

bvanjoi commented Jul 1, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 1, 2025
@petrochenkov
Copy link
Contributor

@bvanjoi
Are you sure this cannot successfully resolve some names that should not be resolved?
(I started reviewing yesterday, but it was late so I'll finish today or on Monday.)

@bvanjoi
Copy link
Contributor Author

bvanjoi commented Jul 12, 2025

Are you sure this cannot successfully resolve some names that should not be resolved?

I need to test this with additional cases across different rib contexts. @rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 12, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 12, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@petrochenkov petrochenkov added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 26, 2025
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Aug 17, 2025

This PR was rebased onto a different master commit! Check out the changes with our range-diff.

@rust-log-analyzer

This comment has been minimized.

github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Aug 18, 2025
resolve: Introduce `RibKind::Block`

to avoid confusing module items, blocks with items, and blocks without items.

Addresses rust-lang/rust#143141 (comment) and rust-lang/rust#143141 (comment).

A couple of related cleanups are also added on top.
@bors

This comment was marked as resolved.

@rustbot

This comment has been minimized.

@bvanjoi
Copy link
Contributor Author

bvanjoi commented Aug 24, 2025

Technically it should do that even if m is exported from a different crate

These changes have wide-ranging implications. Could you please review whether the modifications in tests/ui/hygiene/{legacy_interaction, wrap_unhygienic_example}.rs produce the expected behavior?

@rust-log-analyzer

This comment has been minimized.

@bvanjoi
Copy link
Contributor Author

bvanjoi commented Aug 24, 2025

Reduce for #143141 (comment)

macro_rules! m2 {
    () => {{
        static B: i32 = S;
        42
    }};
}

macro_rules! m {
    () => {
        static S: i32 = m2!();
    };
}

fn main() {
    m!();
}

@bvanjoi
Copy link
Contributor Author

bvanjoi commented Aug 24, 2025

Using lexical scope at macro definition sites presents problematic outcomes:

  1. ​If we adopt this behavior:​​ Risk significant regression, including failure to compile stage2 of the compiler due to tracing crate failures. Additional affected cases exist in tests/ui/hygiene/{wrap_unhygienic_example, legacy_interaction}.rs.

  2. ​If we reject this behavior:​​ The example from fresh binding should shadow the def in expand #143141 (comment) would compile successfully, which contradicts previous discussion.

This behavior is related to the changes: https://github.com/rust-lang/rust/pull/143141/files#diff-c046507afdba3b0705638f53fffa156cbad72ed17aa01d96d7bd1cc10b8d9bceR454-R483

What do you think? Should we introduce this change as a lint instead? @petrochenkov

@petrochenkov petrochenkov added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 25, 2025
@petrochenkov
Copy link
Contributor

Should we introduce this change as a lint instead?

We'll definitely need a crater run and possibly a lint in the end.

But at this point we probably rather need some more or less formal model of the rules that we want to achieve.
At both call site (including code coming from the macro itself) and at def site local variables and items can interleave, so it needs to be clarified what are the priorities.
I.e. what is the search order here.

#![allow(unused)]

fn f() {
    let a = 0;
    {
        fn a() {}
        {
            let a = 0;
            {
                fn a() {}
                
                #[macro_export]
                macro_rules! mac {
                    () => {
                        let a = 0;
                        {
                            fn a() {}
                            {
                                let a = 0;
                                {
                                    a; // use
                                }
                            }
                        }
                    };
                }
            }
        }
    }
}

fn main() {
    let a = 0;
    {
        fn a() {}
        {
            let a = 0;
            {
                mac!();
            }
        }
    }
}

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 25, 2025
@bvanjoi
Copy link
Contributor Author

bvanjoi commented Aug 30, 2025

But at this point we probably rather need some more or less formal model of the rules that we want to achieve.

Have there been any mathematical models proposed, such as proof trees in type theory, to formally describe macro expansion? I've never encountered.😂

@petrochenkov
Copy link
Contributor

@bvanjoi
https://www-old.cs.utah.edu/plt/scope-sets
That's what macros 2.0 hygiene is inspired by as well.

@rustbot
Copy link
Collaborator

rustbot commented Aug 31, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@bvanjoi
Copy link
Contributor Author

bvanjoi commented Aug 31, 2025

what is the search order here.

I will try to answer this after reading this book.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 31, 2025
@rust-log-analyzer

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hygiene of used macro item is weird.
5 participants